# -*- coding: utf-8 -*-
import cv2
import numpy
import socket
import struct

HOST = '192.168.1.33'
PORT = 2022

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # socket对象
# first version:cmd only
# data = bytes([0x55, 0xaa, 0x03, 0x00, 0xc0])
# server.sendto(data, (HOST, PORT))  # 发送指令数据

# second version:
points = [19581, 7521, 24163, 7472, 19145, 10589, 24763, 10490,
          16309, 11876, 27600, 11678, 16145, 16577, 28036, 16428]
# 初始化字节串
byte_array = bytearray([0x55, 0xaa, 0x03])  # 前3个字节

# 将每个点的坐标转换为字节并添加到字节串中
for pt in points:
    # 使用 struct.pack 将整数转换为两个字节，低字节在前
    byte_array.extend(struct.pack('<H', pt))  # '<H' 表示小端的无符号短整型

# 添加最后一个字节
byte_array.append(0xc0)
print("points:", points)
hex_string = ' '.join(f'{byte:02x}' for byte in byte_array)
print("Hex string:", hex_string)
server.sendto(byte_array, (HOST, PORT))  # 发送指令数据


# server.connect((HOST,PORT))
# print('now starting to send frames...')
# capture=cv2.VideoCapture(0) #VideoCapture对象，可获取摄像头设备的数据
# try:
# while True:
#     success,frame=capture.read()
#     while not success and frame is None:
#         success,frame=capture.read()  #获取视频帧
#     frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#     result,imgencode=cv2.imencode('.jpg',frame,[cv2.IMWRITE_JPEG_QUALITY,50])  #编码
#     if imgencode.shape[0]>65535:
#         print(imgencode.shape[0])
#     # server.sendto(struct.pack('i',imgencode.shape[0]),(HOST,PORT)) #发送编码后的字节长度，这个值不是固定的
#     server.sendto(imgencode,(HOST,PORT)) #发送视频帧数据
# print('have sent one frame')
# except Exception as e:
#     print(e)
#     server.sendall(struct.pack('c',1),(HOST,PORT)) #发送关闭消息
#     capture.release()
#     server.close()
